package ru.ipo.dces.client;

import ru.ipo.dces.client.messages.PopupMessage;
import ru.ipo.dces.client.resources.Resources;
import ru.ipo.dces.clientservercommunication.SubmitSolutionRequest;
import ru.ipo.dces.clientservercommunication.SubmitSolutionResponse;
import ru.ipo.dces.debug.PersistentStorage;
import ru.ipo.dces.exceptions.GeneralRequestFailureException;
import ru.ipo.dces.exceptions.ServerReturnedError;
import ru.ipo.dces.pluginapi.Plugin;
import ru.ipo.dces.pluginapi.PluginEnvironment;
import ru.ipo.dces.server.ServerFailReason;
import ru.ipo.dces.utils.ResponseHandler;
import ru.ipo.dces.utils.UIUtils;
import ru.ipo.problemsapi.ExtendedProblem;

import javax.swing.*;
import java.util.HashMap;

public class PluginEnvironmentImpl implements PluginEnvironment {

    private final ExtendedProblem extendedProblem;
    private final int problemID;
    private final String problemName;
    private int tabIndex = -1;
    private String cachedTitle = null;

    private JComponent statementComponent = null;

    private static final Icon TAB_ICON = new ImageIcon(Resources.getInstance().getResourceAsByteArray("TabIcon.gif"));

    private static HashMap<JPanel, Plugin> panel2plugin = new HashMap<JPanel, Plugin>();

    public PluginEnvironmentImpl(int problemID, ExtendedProblem extendedProblem) {
        this.problemID = problemID;
        this.extendedProblem = extendedProblem;
        this.problemName = extendedProblem.getName();
    }

    public PluginEnvironmentImpl(String pluginName) {
        this.problemID = -1;
        this.problemName = pluginName;
        this.extendedProblem = null;
    }

    public void setTitle(String title) {
        if (tabIndex != -1)
            Controller.getClientDialog().getMainTabbedPane().setTitleAt(tabIndex, title);
        else
            cachedTitle = title;
    }

    public void submitSolution(HashMap<String, String> solution) throws GeneralRequestFailureException {
        SubmitSolutionRequest ssr = new SubmitSolutionRequest();
        ssr.problemID = problemID;
        ssr.problemResult = solution;
        ssr.sessionID = Controller.getContestConnection().getSessionID();
        Controller.doRequest(ssr, new ResponseHandler<SubmitSolutionResponse>() {
            @Override
            public void success(SubmitSolutionResponse response) {
            }

            @Override
            public void fail(ServerFailReason exception) {
            }
        }, "Передача решения");
    }

    public ExtendedProblem getProblem() {
        return extendedProblem;
    }

    public JComponent getStatementPanel() {
        if (statementComponent == null)
            statementComponent = UIUtils.getDisplayComponent(extendedProblem, "statement");

        return statementComponent;
    }

    public PersistentStorage getStorage() {
        //TODO return a storage
        return null;
    }

    /*public void log(String message, LogMessageType type) {
        Controller.getLogger().log(
                message,
                type,
                problemName
        );
    }*/

    public void init(Plugin p) {
        panel2plugin.put(p.getPanel(), p);

        JTabbedPane tabbedPane = Controller.getClientDialog().getMainTabbedPane();
        tabbedPane.addTab("Задача №1", TAB_ICON, p.getPanel());
        tabIndex = tabbedPane.getTabCount() - 1;
        if (cachedTitle != null)
            setTitle(cachedTitle);
    }

    public static Plugin getPluginByPanel(JPanel panel) {
        return panel2plugin.get(panel);
    }

}
